<template>
	<view class="fui-wrap">
		<fui-list>
			<fui-list-cell v-for="(item,index) in list" :key="index" arrow @click="clickItem(item,index)">
				<view class="item">
					<view class="line">
						<fui-text text="商户：" color="#afafaf" :size="28"></fui-text>
						<fui-text :text="item.merch_name" color="#000" :size="28"></fui-text>
					</view>
					<view class="line">
						<fui-text :text="item.settle_type=='after'?'账单：':'余额：'" color="#afafaf" :size="28"></fui-text>
						<merch-balance :balance="item.balance" :settle_type="item.settle_type"></merch-balance>
					</view>
				</view>
				<view class="status">
					<fui-tag v-if="item.status=='normal'" text="正常" type="success"></fui-tag>
					<fui-tag v-if="item.status=='hidden'" text="禁用" type="danger"></fui-tag>
				</view>
			</fui-list-cell>
		</fui-list>
		<fui-empty v-if="list.length===0" :src="`${baseUrl}assets/img/miniapp/empty.png`" title="暂无数据!" :marginTop="200"></fui-empty>
		<fui-modal :show="act.show" descr="请选择操作" v-if="buttons" :buttons="buttons" :radius="16" :maskClosable="true" direction="column" @click="clickButton" @cancel="cancelButton"></fui-modal>
		<filter :param="param" @confirm="doSubmit"></filter>
	</view>
</template>

<script>
	import {methods,baseUrl} from '@/utils/core.js';
	import {formatDuration,parseNumber} from '@/utils/util.js'
	import filter from '@/components/common/filter.vue';
	import merchBalance from '@/components/common/merch-balance.vue';
	export default {
		components:{
			filter:filter,
			merchBalance:merchBalance
		},
		data() {
			return {
				baseUrl:baseUrl,
				ziying:'',
				list:[],
				page:1,
				endData:false,
				act:{
					show:false,
					index:'',
					item:''
				},
				buttons:'',
				param:{
					merch_name:'',
				}
			}
		},
		onLoad(e) {
			if(e.ziying && e.ziying==1){
				this.ziying=1;
				uni.setNavigationBarTitle({
					title:'自营商户'
				});
			}
			this.getList();
		},
		onShow() {
			let refresh=uni.getStorageSync('refresh') || '';
			if(refresh){
				this.list=[];
				this.page=1;
				this.endData=false;
				uni.setStorageSync('refresh','');
				this.getList();
			}
		},
		onReachBottom() {
			this.getList();
		},
		onPullDownRefresh() {
			this.list=[];
			this.page=1;
			this.endData=false;
			this.getList(function(){
				uni.stopPullDownRefresh();
			});
		},
		methods: {
			...methods,
			formatDuration,
			parseNumber,
			clickItem:function(item,index){
				this.buttons=[
					{
						text: '编辑',
						background: '#22b2ff',
						value:'edit'
					},{
						text: '充值',
						background: '#1ab445',
						value:'recharge'
					},{
						text: '账单',
						background: '#dc9300',
						value:'log'
					},{
						text: '删除',
						background: '#ff0000',
						value:'delete'
					}
				]
				this.act={
					show:true,
					item:item,
					index:index
				};
			},
			doSubmit:function(param){
				this.param=param;
				this.list=[];
				this.page=1;
				this.endData=false;
				this.getList();
			},
			getList:function(callback=false){
				this.showPopup=false;
				if(this.endData){
					this.$msg('已经到底了');
					return;
				}
				this.$get('parking/merch/list',{
					...this.param,
					ziying:this.ziying,
					page:this.page
				},true).then(res=>{
					this.list=this.list.concat(res);
					this.page++;
					if(res.length<10){
						this.endData=true;
					}	
					if(callback){
						callback();
					}
				});
			},
			clickButton:function(e){
				let id=this.act.item.id;
				let index=this.act.item.index;
				let settle_type=this.act.item.settle_type;
				if(e.value=='edit'){
					this.$navigateTo('parking/merch/detail?merch_id='+id);
				}
				if(e.value=='recharge'){
					this.$navigateTo('parking/merch/recharge?merch_id='+id);
				}
				if(e.value=='log'){
					this.$navigateTo('parking/merch/log?settle_type='+settle_type+'&merch_id='+id);
				}
				if(e.value=='delete'){
					uni.showModal({
						content:'您确定要删除该商户吗？',
						success:(e)=>{
							if(e.confirm){
								this.$post('parking/merch/del',{merch_id:id},true,true).then(res=>{
									this.list.splice(index,1);
								});
							}
						}
					})
				}
				this.cancelButton();
			},
			cancelButton:function(){
				this.buttons='';
				this.act={
					show:false,
					index:'',
					item:''
				};
			}
		}
	}
</script>

<style lang="scss" scoped>
.item{
	width: 100%;
	.line{
		display: flex;
		height: 60rpx;
		line-height: 60rpx;
	}
}
</style>